package org.example.cdpcrpbackend.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.example.cdpcrpbackend.controller.dto.ActivityAttend2;
import org.example.cdpcrpbackend.controller.dto.ApplyCrp;
import org.example.cdpcrpbackend.controller.dto.Result;
import org.example.cdpcrpbackend.domain.Activity;
import org.example.cdpcrpbackend.domain.ActivityAttend;
import org.example.cdpcrpbackend.service.ActivityAttendService;
import org.example.cdpcrpbackend.service.ActivityService;
import org.example.cdpcrpbackend.service.ScoreRecordService;
import org.example.cdpcrpbackend.util.CodeTool;
import org.example.cdpcrpbackend.util.MyType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/score-record")
@RequiredArgsConstructor
public class ScoreRecordController {
    private final ScoreRecordService recordService;
    private final ActivityAttendService attendService;
    private final ActivityService activityService;


    /**
     * 学生手动加分
     * ⑤学生手动加分
     * method5
     */
    @PostMapping("/apply")
    public Result apply(@RequestBody ApplyCrp applyCrp) {
        if (recordService.apply(applyCrp)) {
            return Result.success();
        } else {
            return Result.fail("提交失败");
        }
    }

    /**
     * 学生扫活动二维码
     * ①签到一次加分
     * ②签到、签退加分
     */
    @PostMapping("/method1-2")
    public Result method1_2(@RequestBody ActivityAttend2 dto) {
        // 获取 二维码code
        String code = dto.getCode();
        // 判断 二维码是否正确
        if (!CodeTool.verify(code)) {
            return Result.fail("无效的二维码");
        }
        // 确保活动不能过期
        String activityId = CodeTool.getUid(code);
        LambdaQueryWrapper<Activity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Activity::getId, activityId);
        wrapper.gt(Activity::getEndTime, LocalDateTime.now());
        Activity actDB = activityService.getOne(wrapper);
        if (actDB == null) {
            return Result.fail("活动不存在或已经结束");
        }

        // 拷贝属性
        ActivityAttend attend = new ActivityAttend();
        BeanUtil.copyProperties(dto, attend);
        // 判断活动参与方式
        String method = actDB.getMethod();
        if (method.equals(MyType.M1)) {
            String msg = attendService.attendByM1(attend);
            // msg：签到成功、请勿重复签到
            return Result.successMsg(msg);
        } else if (method.equals(MyType.M2)) {
            String msg = attendService.attendByM2(attend);
            // msg：1签到成功、2签退成功、请勿重复签退
            return Result.successMsg(msg);
        } else {
            return Result.fail("不支持扫码");
        }
    }

    /**
     * 仅报名
     * ③仅报名
     */
    @PostMapping("/method3")
    public Result method3(@RequestBody ActivityAttend activityAttend) {
//        System.out.println(activityAttend);
        /*
        1、学生点击报名按钮，
        2、在 tb_activity_attend 表中添加记录 sign_in_out：已报名；state：未加分
        3、不在 tb_score_record 记录，因为不知道具体分值
        */
        // 禁止重复报名
        LambdaQueryWrapper<ActivityAttend> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ActivityAttend::getActivityId, activityAttend.getActivityId());
        wrapper.eq(ActivityAttend::getStudentId, activityAttend.getStudentId());
        ActivityAttend one = attendService.getOne(wrapper);


        System.out.println(one);

        if (one != null) {
            return Result.fail("已报名");
        }

        attendService.attendByM3(activityAttend);
        return Result.success();
    }
}
